package com.yiren.dbaa.module.exam.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiren.backstage.currency.entity.GradeStandardEntity;
import com.yiren.backstage.currency.utils.StringUtil;
import com.yiren.dbaa.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by WANGRONGXIANG
 * on 2022/3/1
 * Describe 滴驾人工评判适配器
 */
public class DjManualJudgeAdapter extends BaseAdapter {

    private Context context;
    private List<GradeStandardEntity> gradeStandards;
    private int mode;   //1:白天模式   2:夜间模式
    private OnJudgeCheckChangeListener listener;
    private int checkPosition = -1;

    public DjManualJudgeAdapter(Context context, List<GradeStandardEntity> gradeStandards, int mode, OnJudgeCheckChangeListener listener) {
        this.context = context;
        this.gradeStandards = gradeStandards;
        this.mode = mode;
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return gradeStandards.size();
    }

    @Override
    public Object getItem(int position) {
        return gradeStandards.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_dj_manual_judge, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (position == checkPosition) {
            holder.cbSelect.setChecked(true);
        } else {
            holder.cbSelect.setChecked(false);
        }
        holder.tvProject.setText(StringUtil.getProjectName(gradeStandards.get(position).getProjectEntity().getName()));
        holder.tvCause.setText(gradeStandards.get(position).getStandardContent());
        holder.tvScore.setText(gradeStandards.get(position).getGrade() + "");
        if (mode == 1) {
            holder.tvProject.setTextColor(context.getResources().getColor(R.color.exam_txt));
            holder.tvCause.setTextColor(context.getResources().getColor(R.color.exam_txt));
            holder.tvScore.setTextColor(context.getResources().getColor(R.color.exam_txt));
        } else {
            holder.tvProject.setTextColor(context.getResources().getColor(R.color.white));
            holder.tvCause.setTextColor(context.getResources().getColor(R.color.white));
            holder.tvScore.setTextColor(context.getResources().getColor(R.color.white));
        }

        convertView.setOnClickListener(v -> {
            setCheckPosition(position);
            listener.onJudgeCheckChange(position);
        });
        return convertView;
    }

    public void setCheckPosition(int checkPosition) {
        this.checkPosition = checkPosition;
        notifyDataSetChanged();
    }

    public void setMode(int mode) {
        this.mode = mode;
        notifyDataSetChanged();
    }

    static class ViewHolder {
        @BindView(R.id.tv_project)
        TextView tvProject;
        @BindView(R.id.tv_cause)
        TextView tvCause;
        @BindView(R.id.tv_score)
        TextView tvScore;
        @BindView(R.id.cb_select)
        CheckBox cbSelect;
        @BindView(R.id.ll_select)
        LinearLayout llSelect;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    /**
     * 人工评判选择改变的事件
     */
    public interface OnJudgeCheckChangeListener {

        void onJudgeCheckChange(int position);
    }

}
